package york.Game;

public class Matrix3x3 {
	double _11,_12,_13;
	double _21,_22,_23;
	double _31,_32,_33;
	
	public Matrix3x3() {
		Identity();
	}
	
	public void Identity() {
		_11 = 1; _12 = 0; _13 = 0;
		_21 = 0; _22 = 1; _23 = 0;
		_31 = 0; _32 = 0; _33 = 1;
	}
	
	public void set(Matrix3x3 a_Mat) {
		_11 = a_Mat._11; _12 = a_Mat._12; _13 = a_Mat._13;
		_21 = a_Mat._21; _22 = a_Mat._22; _23 = a_Mat._23;
		_31 = a_Mat._31; _32 = a_Mat._32; _33 = a_Mat._33;
	}
	
	public void Multiply(Matrix3x3 a_Mat) {
		Matrix3x3 hold = new Matrix3x3();
		hold._11 = (this._11*a_Mat._11) + (this._12*a_Mat._21) + (this._13*a_Mat._31);
		hold._12 = (this._11*a_Mat._12) + (this._12*a_Mat._22) + (this._13*a_Mat._32);
		hold._13 = (this._11*a_Mat._13) + (this._12*a_Mat._23) + (this._13*a_Mat._33);

		hold._21 = (this._21*a_Mat._11) + (this._22*a_Mat._21) + (this._23*a_Mat._31);
		hold._22 = (this._21*a_Mat._12) + (this._22*a_Mat._22) + (this._23*a_Mat._32);
		hold._23 = (this._21*a_Mat._13) + (this._22*a_Mat._23) + (this._23*a_Mat._33);
		
		hold._31 = (this._31*a_Mat._11) + (this._32*a_Mat._21) + (this._33*a_Mat._31);
		hold._32 = (this._31*a_Mat._12) + (this._32*a_Mat._22) + (this._33*a_Mat._32);
		hold._33 = (this._31*a_Mat._13) + (this._32*a_Mat._23) + (this._33*a_Mat._33);
		this.set(hold);
		hold = null;
	}
	
	public void Translate(double x,double y) {
		Matrix3x3 NewMat = new Matrix3x3();
		NewMat._11 = 1; NewMat._12 = 0; NewMat._13 = 0;
		NewMat._21 = 0; NewMat._22 = 1; NewMat._23 = 0;
		NewMat._31 = x; NewMat._32 = y; NewMat._33 = 1;
		Multiply(NewMat);
	}
	
	public void Scale(double xS,double yS) {
		Matrix3x3 NewMat = new Matrix3x3();
		NewMat._11 = xS; NewMat._12 = 0; NewMat._13 = 0;
		NewMat._21 = 0; NewMat._22 = yS; NewMat._23 = 0;
		NewMat._31 = 0; NewMat._32 = 0; NewMat._33 = 1;
		Multiply(NewMat);
	}
	
	//Rotates by degrees
	public void Rotate(double deg) {
		Matrix3x3 NewMat = new Matrix3x3();
		
		double S = Math.sin(deg);
		double C = Math.cos(deg);
		
		NewMat._11 = C; NewMat._12 = S; NewMat._13 = 0;
		NewMat._21 = -S; NewMat._22 = C; NewMat._23 = 0;
		NewMat._31 = 0; NewMat._32 = 0; NewMat._33 = 1;
		
		Multiply(NewMat);
	}
	
	//Rotates based on a vector
	public void Rotate(Vector2D head, Vector2D side) {
		Matrix3x3 NewMat = new Matrix3x3();
		
		NewMat._11 = head.x; NewMat._12 = head.y; NewMat._13 = 0;
		NewMat._21 = side.x; NewMat._22 = side.y; NewMat._23 = 0;
		NewMat._31 = 0; NewMat._32 = 0; NewMat._33 = 1;
		
		Multiply(NewMat);
	}
	
	public Vector2D TransformVector2D(Vector2D a_Vec) {
		double tX = (_11*a_Vec.x) + (_21*a_Vec.y) + (_31);
		double tY = (_12*a_Vec.x) + (_22*a_Vec.y) + (_32);
		return new Vector2D(tX,tY);
	}
}
